package org.example;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.example.entity.User;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author laoma
 * @create 2021-06-06 13:43
 */
public class TestPoi {

    // 获取所有数据
    public List<User> getUsers(){
        ArrayList<User> users = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setId(String.valueOf(i))
                    .setName("ma_"+i)
                    .setAge(i+10)
                    .setBir(new Date());
            users.add(user);
        }
        return users;
    }

    @SneakyThrows
    @Test
    public void testExport(){
        // 获取数据
        List<User> users = getUsers();
        // 导出excel
        // 参数1：exportParams 导出配置对象 参数2：导出配置类型 参数3：导出数据集合
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户信息列表", "用户信息", ExcelType.XSSF), User.class, users);
        // 将excel写入指定的路径
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\java-workspace\\malearn\\easypoi\\aa.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        workbook.close();
    }

    @SneakyThrows
    @Test
    public void testImport(){
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<User> list = ExcelImportUtil.importExcel(new FileInputStream("F:\\java-workspace\\malearn\\easypoi\\aa.xlsx"), User.class, params);
        list.forEach(System.out::println);
    }
}
